<?php
namespace App\Traits;

use App\Http\Controllers\HandleRedis;
use App\Models\jiqiren_content;
use App\Models\jiqiren_user;
use App\Models\u_xianhong;

//资源加载(初始化数据)
trait StartInitParam
{
    private static $openTime = 60;//自定义开奖时间

    private static $AllXianhong = [];//所有限红

    private static $JiqirenUser = [];//所有机器人用户

    private static $JiqirenContent = [];//所有机器人下注信息

    private static $AllResultData = [];//所有开奖数据(根据台号区分)

    private static $AllResultStatus = [];//所有开奖的状态

    private static $AllResultTime = [];//所有开奖的时间

    public function __construct()
    {
        self::init();
    }

    //初始化参数
    protected static function init()
    {
        self::allXianhong();//所有限红
        self::allJiqirenContent();//先获取投注
        self::allJiqiren();//再处理机器人用户
        self::setAllResultData();//设置开奖数据

    }

    /**
     * 初始化限红数据
     */
    protected static function allXianhong()
    {
        $xianhongData = HandleRedis::getAllXianhong();
        if($xianhongData) {
            self::$AllXianhong = $xianhongData;
        } else {
            self::$AllXianhong = u_xianhong::getAllXianhong();
            HandleRedis::setAllXianhong(self::$AllXianhong);
        }
    }

    /**
     * 初始化机器人用户数据
     */
    protected static function allJiqiren()
    {
        $robotData = HandleRedis::getAllRobot();
        if($robotData) {
            self::$JiqirenUser = $robotData;
        } else {
            self::$JiqirenUser = jiqiren_user::getAllJiqiren();//查库
            self::$JiqirenContent = createArrayByColumn(self::$JiqirenContent,'pid');
            foreach (self::$JiqirenUser as $k=>$v) {
                self::$JiqirenUser[$k]['content'] = toJson(isset(self::$JiqirenContent[$v['id']])?self::$JiqirenContent[$v['id']]:[]);
            }
            HandleRedis::setAllRobot(self::$JiqirenUser);//设置redis
        }
        foreach (self::$JiqirenUser as $k=>$v) self::$JiqirenUser[$k]['content'] = toArray($v['content']);
    }

    /**
     * 初始化机器人投注信息
     */
    protected static function allJiqirenContent()
    {
        $content = HandleRedis::getAllRobotBetContent();
        if($content) {
            self::$JiqirenContent = $content;
        }else{
            self::$JiqirenContent = jiqiren_content::getAllContent('pid,content');
            HandleRedis::setAllRobotBetContent(self::$JiqirenContent);
        }
    }

    /**
     * 设置所有开奖数据
     */
    protected static function setAllResultData()
    {
        //每秒定时采集一次
        \App\Libs\Timer::add(1,function (){
            self::$AllResultData = HandleRedis::getAllResultData();//所有开奖数据
            self::$AllResultStatus = HandleRedis::getTableStatus();//所有开奖状态
            self::$AllResultTime = HandleRedis::getAllTableOpenTime();//所有开奖时间
        },__FUNCTION__);//可配置key(作用与执行的时候可选择运行的定时器key,空则忽略key运行所有)
    }
}
